package com.hzx.ssm.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hzx.ssm.dao.MessageDao;
import com.hzx.ssm.entity.Message;
import com.hzx.ssm.entity.ResBean;
import com.hzx.ssm.service.MessageService;
import com.hzx.ssm.util.SortMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    private MessageDao messageDao;

    @Override
    public HashMap<String, Object> findAllIsMessage(Integer pageNum, Integer sizeNum) {
        Page<Message> page = PageHelper.startPage(pageNum, sizeNum);
        List<Message> allMessage = messageDao.findAllIsMessage();
        allMessage = SortMessage.sort(allMessage);
        int pages = page.getPages();
        HashMap<String, Object> data = new HashMap<>();
        data.put("allMessage", allMessage);
        data.put("pagesNum", pages);
        return data;
    }

    @Override
    public HashMap<String, Object> findAllIsNotMessage(Integer pageNum, Integer sizeNum) {
        Page<Message> page = PageHelper.startPage(pageNum, sizeNum);
        List<Message> allMessage = messageDao.findAllIsNotMessage();
        allMessage = SortMessage.sort(allMessage);
        int pages = page.getPages();
        HashMap<String, Object> data = new HashMap<>();
        data.put("allMessage", allMessage);
        data.put("pagesNum", pages);
        return data;
    }

    @Override
    public void deleteMessageById(String message_id) {
        messageDao.deleteMessageById(message_id);
    }

    @Override
    public void addMessage(Message message) {
        message.setMessage_id(UUID.randomUUID().toString());
        message.setMessage_upNum("0");
        message.setMessage_time(new Date());
        //enable=0为不可见
        message.setMessage_enable(0);
        messageDao.addMessage(message);
    }

    public void setMessageEnable(String message_id) {
        messageDao.setMessageEnable(message_id);
    }


    @Override
    public ResBean deleteMyMessage(String message_id, String user_id) {
        Message message = messageDao.MessageToUser(message_id, user_id);
        if(message == null)
            return ResBean.error(401,"删除失败，异常操作");
        messageDao.deleteMessageById(message_id);
        return ResBean.sucess(200,"删除成功");
    }

    public ResBean findMyAllMessage(String user_id,int pageNum,int sizeNum){
        Page<Message> startPage = PageHelper.startPage(pageNum, sizeNum);
        List<Message> messages = messageDao.findMyAllMessage(user_id);
        messages = SortMessage.sort(messages);
        int pages = startPage.getPages();
        HashMap<Object, Object> data = new HashMap<>();
        data.put("messages",messages);
        data.put("pages",pages);
        return ResBean.sucess(200,"查找自己的所有消息成功",data);
    }

    @Override
    public ResBean updateUpNum(String message_id) {
        String upNum = messageDao.findUpNum(message_id);
        String res = Integer.parseInt(upNum)+1+"";
        messageDao.updateUpNum(message_id,res);
        return ResBean.sucess(200,"点赞成功");
    }


}
